<h1><?php echo $title_for_layout = __d('lil_ads', 'List Campaigns', true); ?></h1>
<div class="index">
	<div class="filter">
	<?php
		echo $this->Html->link(
			__d('lil_ads', 'Show', true),
			'javascript:void(0);',
			array('class'=>'popup_link', 'id'=>'popup_link_filter')
		);
		
		echo ' ' . __d('lil_ads', 'all', true) . ' ';
		
		if (isset($params['active']) && $params['active'] !== 'NULL') {
			echo $this->Html->link(
				array_search(
					$params['active'],
					array(
						__d('lil_ads', 'active', true) => 1,
						__d('lil_ads', 'inactive', true) => 0
					)
				),
				'javascript:void(0);',
				array('class' => 'popup_link', 'id' => 'popup_link_active')
			) . ' ';
		}
		__d('lil_ads', 'campaigns');
		
		if (isset($params['priority']) && $params['priority']!=='NULL') {
			echo ' ' . __d('lil_ads', 'with', true) . ' ';
			echo $this->Html->link(
				array_search(
					$params['priority'],
					array('low' => 1, 'medium' => 2, 'high' => 3)
				),
				'javascript:void(0);',
				array('class'=>'popup_link', 'id'=>'popup_link_priority')
			).' ';
			echo __d('lil_ads', 'priority', true);
		}
		echo '.';
	?>
	</div>
	<div class="popup popup_link_filter" style="display:none;">
		<ul>
			<li><?php echo $this->Html->link(__d('lil_ads', 'Active', true), array_merge($params, array('active' => 1))); ?></li>
			<li><?php echo $this->Html->link(__d('lil_ads', 'Inactive', true), array_merge($params, array('active' => 0))); ?></li>
			<li><hr /></li>
		</ul><ul id="PopupTreePriority">
			<li><b><?php __d('lil_ads', 'Priority'); ?></b>
				<ul>
					<li><?php echo $this->Html->link('Low', array_merge($params, array('priority' => 1))); ?></li>
					<li><?php echo $this->Html->link('Normal', array_merge($params, array('priority' => 2))); ?></li>
					<li><?php echo $this->Html->link('High', array_merge($params, array('priority' => 3))); ?></li>
				</ul>
			</li>
		</ul><ul>
			<li><hr /></li>
			<li><?php echo $this->Html->link(__d('lil_ads', 'All', true), array('filter' => 'all')); ?></li>
		</ul>
	</div>
	<div class="popup popup_link_active" style="display:none;">
		<ul>
			<li><?php echo $this->Html->link(__d('lil_ads', 'Active', true), array_merge($params, array('active' => 1))); ?></li>
			<li><?php echo $this->Html->link(__d('lil_ads', 'Inactive', true), array_merge($params, array('active' => 0))); ?></li>
			<li><hr /></li>
			<li><?php echo $this->Html->link(__d('lil_ads', 'All', true), array_merge($params, array('active' => 'NULL'))); ?></li>
		</ul>
	</div>
	<div class="popup popup_link_priority" style="display:none;">
		<ul>
			<li><?php echo $this->Html->link(__d('lil_ads', 'Low', true), array_merge($params, array('priority' => 1))); ?></li>
			<li><?php echo $this->Html->link(__d('lil_ads', 'Normal', true), array_merge($params, array('priority' => 2))); ?></li>
			<li><?php echo $this->Html->link(__d('lil_ads', 'High', true), array_merge($params, array('priority' => 3))); ?></li>
			<li><hr /></li>
			<li><?php echo $this->Html->link(__d('lil_ads', 'All', true), array_merge($params, array('priority' => 'NULL'))); ?></li>
		</ul>
	</div>
	<br />
	<table cellpadding="0" cellspacing="0">
	<tr>
		<th class="left"><?php echo $this->Paginator->sort(__d('lil_ads', 'AdsCampaign', true), 'title');?><br /><span class="sub"><?php __d('lil_ads', 'Ad'); ?></span></th>
		<th class="center"><?php echo $this->Paginator->sort(__d('lil_ads', 'Priority', true), 'priority');?><br /><span class="sub"><?php __d('lil_ads', 'Size'); ?></span></th>
		<th class="center"><?php echo $this->Paginator->sort(__d('lil_ads', 'Click Count', true), 'clicks');?></th>
		<th class="center"><?php echo $this->Paginator->sort(__d('lil_ads', 'Display Count', true), 'views');?></th>
		<th class="center"><?php echo __d('lil_ads', 'CTR [%]', true); ?></th>
		<?php if (empty($stats)) { ?>
			<th class="center">&nbsp;</th>
			<th class="center">&nbsp;</th>
			<th class="center">&nbsp;</th>
			<th class="center">&nbsp;</th>
		<?php } ?>
	</tr>
<?php
	$i = 0; 
	$priority = array(
		1 => __d('lil_ads', 'low', true),
		2 => __d('lil_ads', 'medium', true),
		3 => __d('lil_ads', 'high', true)
	);
	$banner_kinds = array(
		'I' => __d('lil_ads', 'image', true),
		'F' => __d('lil_ads', 'flash', true),
		'P' => __d('lil_ads', 'plain', true),
		'H' => __d('lil_ads', 'html', true)
	);
	
	foreach ($campaigns as $campaign) {
		echo '<tr id="row_' . $campaign['AdsCampaign']['id'].'"'.(($i++ % 2 == 0)?' class="altrow"':'') . '>';
		
		echo '<td' . ((!$campaign['AdsCampaign']['active']) ? ' class="deleted"' : '') . '>';
		if (empty($stats)) {
			echo '<span class="big strong">' . $this->Html->clean($campaign['AdsCampaign']['title']) . '</span>';
		} else {
			echo $this->Html->link(
				$campaign['AdsCampaign']['title'],
				array(
					'admin'       => true,
					'controller'  => 'ads_actions',
					'action'      => 'daily',
					'campaign_id' => $campaign['AdsCampaign']['id']
				),
				array('class' => 'big strong')
			);	
		}
		echo '</td>';
		
		printf('<td class="%1$s">%2$s</td>',
			'center' . ((!$campaign['AdsCampaign']['active']) ? ' deleted' : ' strong'),
			$this->Html->clean($priority[$campaign['AdsCampaign']['priority']])
		);
		
		printf('<td class="%1$s">%2$d</td>',
			'center' . ((!$campaign['AdsCampaign']['active']) ? ' deleted' : ' strong'),
			$campaign['AdsCampaign']['clicks']
		);
		
		printf('<td class="%1$s">%2$d</td>',
			'center' . ((!$campaign['AdsCampaign']['active']) ? ' deleted' : ' strong'),
			$campaign['AdsCampaign']['views']
		);
		
		printf('<td class="%1$s">%2$.2f</td>',
			'center' . ((!$campaign['AdsCampaign']['active']) ? ' deleted' : ' strong'),
			($campaign['AdsCampaign']['views'] > 0) ? ($campaign['AdsCampaign']['clicks'] / $campaign['AdsCampaign']['views'] * 100) : 0
		);
		
		
		if (empty($stats)) {
			if (!$campaign['AdsCampaign']['active']) {
				echo 
					'<td class="center" colspan="4">' .
					$this->Html->link(__d('lil_ads', 'Activate', true), array(
						'admin'  => true,
						'action' => 'activate',
						$campaign['AdsCampaign']['id']
					)) .
					'</td>';
			} else {
				echo '<td class="center">' . $this->Html->link(__d('lil_ads', 'Update', true), array('admin' => true, 'action'=>'refresh', $campaign['AdsCampaign']['id'])) . '</td>';
				echo '<td class="center">' . $this->Html->link(__d('lil_ads', 'Edit', true), array('admin' => true, 'action'=>'edit', $campaign['AdsCampaign']['id'])) . '</td>';
				echo '<td class="center">' . $this->Html->link(__d('lil_ads', 'Delete', true), array('admin' => true, 'action'=>'delete', $campaign['AdsCampaign']['id'])) . '</td>';
				echo '<td class="center">' . $this->Html->link(__d('lil_ads', 'Add an Ad', true), array('admin' => true, 'controller'=>'ads_banners', 'action'=>'add', 'AdsCampaign'=>$campaign['AdsCampaign']['id'])) . '</td>';
			}
			echo '</td>';
			echo '</tr>';
		}
		
		if ($campaign['AdsCampaign']['active']) {
			foreach ($campaign['AdsBanner'] as $banner) {
				echo '<tr'.(($i % 2 == 0)?' class="altrow"':'').'>';
				echo '<td class="sub">'.'['.$banner_kinds[$banner['kind']].'] ';
				if (empty($stats)) {
					echo $this->Html->clean($banner['title']);
				} else {
					echo $this->Html->link($banner['title'], array(
						'admin'       => true, 
						'controller'  => 'ads_actions',
						'action'      => 'daily',
						'campaign_id' => $campaign['AdsCampaign']['id'],
						'banner_id'   => $banner['id']
					));
				}
				echo '</td>';
				echo '<td align="center">'.((!empty($banner['width']))?($banner['width'].'x'.$banner['height'].'px'):'N/A').'</td>';
				echo '<td></td>';
				echo '<td></td>';
				echo '<td></td>';
				if (empty($stats)) {
					echo '<td>&nbsp;</td>';
					echo '<td class="center">' . $this->Html->link(__d('lil_ads', 'Edit', true), array('admin' => true, 'controller'=>'ads_banners', 'action'=>'edit', $banner['id'])) . '</td>';
					echo '<td class="center">' . $this->Html->link(__d('lil_ads', 'Delete', true), array('admin' => true, 'controller'=>'ads_banners', 'action'=>'delete', $banner['id'])) . '</td>';
					echo '<td>&nbsp;</td>';
				}	
				echo '</tr>';
			}
		}
	}
?>
		<tfoot>
			<tr>
				<td colspan="9" class="paging"><?php echo $this->Lil->paginator(); ?></td>
			</tr>
		</tfoot>
	</table>

</div>